﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Honestysoft.Financing.Model;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.BaseObject;

namespace Honestysoft.Financing.WebUI.Module.Project
{
    public partial class ProjectSplit : BasePage
    {     
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                Bind();
            }
        }
        /// <summary>
        /// 根据权限控制页面按钮.
        /// 基类中已自动做控制。
        /// </summary>
        protected override void ButtonSetting()
        {
            base.ButtonSetting();
            //使用PageSecurity检查权限
            if (PageSecurity.Add)
            {
                btnAdd.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Project/SelectSplitProject.aspx?FunctionID=" + FunctionID + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增拆分项目设置');return false;");
            }
        }
        DataView dvDetail = null;

        private void Bind()
        {
            if (dvDetail == null)
            {
                dvDetail = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetSplitProject().DefaultView;
            }
            DataTable dt = new Honestysoft.BaseObject.BaseBusinessLogic<BD_ProjectSplit>(Globals.CurrentConnStringName).GetTable();
            dlSplit.DataSource = dt;
            dlSplit.DataBind();
        }

        protected void dlSplit_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
            Repeater rptProject = e.Item.FindControl("rptProject") as Repeater;
            if (drv != null && rptProject != null)
            {
                if (dvDetail != null)
                {
                    dvDetail.RowFilter = "SplitID='" + drv["ID"].ToString() + "'";
                    rptProject.DataSource = dvDetail;
                    rptProject.DataBind();
                }
            }
        }

        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arrId = GetSelectedRowKeys();
                BaseBusinessLogic<BD_ProjectSplit> baseLogic = new BaseBusinessLogic<BD_ProjectSplit>();
                baseLogic.DeleteByKey(arrId);
                ScriptHelper.Window.Alert("删除成功！");
                Bind();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("删除失败！系统发生了未知错误！");
            }
        }

        protected string GetProjectUrl(string id, string text)
        {
            return ScriptHelper.LinkButton.GetGridEditLink(text, Globals.Home + "Module/Project/BD_Project_View.aspx", "查看项目信息", ModalDialogSize.ViewWidth, ModalDialogSize.ViewHeight, id, FunctionID);
        }

        protected void btnDeleteSplit_Click(object sender, EventArgs e)
        {
            int ret = new Honestysoft.BaseObject.BaseBusinessLogic<BD_ProjectSplit_Detail>().DeleteByKey(hidDeleteID.Value);
            hidDeleteID.Value = "";
            if (ret == 1)
            {
                ScriptHelper.Window.Alert("删除成功！");
                Bind();
            }
            else
            {
                ScriptHelper.Window.Alert("删除失败！");
            }
        }
    }
}